package com.situ.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Course;
import com.situ.web.service.ICourseService;
import com.situ.web.service.impl.CourseServiceImpl;
import com.situ.web.util.JSONResult;
import com.situ.web.util.JSONUtil;
import com.situ.web.vo.PageInfo;

// 用来处理客户端的请求、响应动态资源给浏览器
// 你继承什么你就是什么
// java.lang.IllegalArgumentException: Invalid <url-pattern> [course] in servlet mapping
// http://localhost:8080/Java202007Web/course
@WebServlet("/course")
public class CourseServlet extends HttpServlet{
	private ICourseService courseService = new CourseServiceImpl();

	// req:浏览器请求过来的数据都封装到reqest
	// resp: 服务器响应动态资源给浏览器
	// 访问servlet时候默认访问service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("CourseServlet.service()");
		//req.setCharacterEncoding("UTF-8");
		
		// 判断用户有没有登录，如果已经登录就继续往下访问，没有登录跳转到登录界面
//		HttpSession session = req.getSession();
//		User user = (User) session.getAttribute("user");
//		if (user == null) {
//			resp.sendRedirect(req.getContextPath() + "/login.jsp");
//			return;
//		}
		
		String method = req.getParameter("method");
		// 如果这个method=null或者是""字符串，就让这么method的值为selectAll
		if (method == null || "".equals(method)) {
			method = "selectByPage";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectByPage":
			selectByPage(req, resp);
			break;
		case "selectLayuiByPage":
			selectLayuiByPage(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "getCourseInsertPage":
			getCourseInsertPage(req, resp);
			break;
		default:
			break;
		}
	}

	private void getCourseInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.getRequestDispatcher("/course_insert.jsp").forward(req, resp);
	}

	private void selectLayuiByPage(HttpServletRequest req, HttpServletResponse resp) {
		// /course?method=selectLayuiByPage&page=1&limit=10
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		
		JSONResult jsonResult = courseService.selectLayuiByPage(Integer.parseInt(page), Integer.parseInt(limit));
		
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		
		boolean isSuccess = courseService.deleteById(Integer.parseInt(id));
		// {'isSuccess' : true}
		//Map<String, Object> map = new HashMap<String, Object>();
		//map.put("isSuccess", isSuccess);
		//map.put("msg", isSuccess ? "删除成功" : "删除失败");
		// JSONUtil.obj2Json(map, resp);
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			// {'code':0, msg:'删除成功'}
			jsonResult = new JSONResult(JSONResult.OK, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
		
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("CourseServlet.selectByPage()");
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		// List<Course> list;
		PageInfo pageInfo = courseService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/course_page_list.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("CourseServlet.insert()");
		// 1、得到前台传递过来的数据
		String name = req.getParameter("name");
		String credit = req.getParameter("credit");
		// 封装成Course对象
		Course course = new Course(name, Integer.parseInt(credit));
		// 2、调用service代码完成功能
		boolean isSuccess = courseService.insert(course);
		System.out.println(isSuccess);
		// 3、界面跳转
		// resp.sendRedirect(req.getContextPath() + "/course?method=selectAll");
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.OK, "添加成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "添加失败");
		}
		
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("CourseServlet.selectAll()");
		List<Course> list = courseService.selectAll();
		
		// 把list集合放到req这个内存区域里面
		req.setAttribute("list", list);
		// 转发（跳转）到course_list.jsp页面进行展示
		req.getRequestDispatcher("course_list.jsp").forward(req, resp);
	}
}
